package com.animations;

import com.beans.BeginBean;
import com.beans.BirdBean;
import com.beans.ButtonEvent;
import creator.ui.controller.VoicePlayer;
import creator.ui.graphics.Magicanvas;
import creator.ui.graphics.bean.Switch;
import creator.ui.graphics.polaroid.Animation;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.PrintStream;
import java.util.ArrayList;

public class AgainAction extends Animation {

  private BeginBean begin;

  public void loadBean() {
    this.begin = new BeginBean();
    getBeans().add(this.begin);
  }

  public void paint(Graphics g) {
    if (!BirdBean._isAlive()) g.drawImage(
      this.begin.getBegin(),
      BeginBean.getBeginX(),
      BeginBean.getBeginY(),
      getMagicanvas()
    );
  }

  public void run() {
    getMagicanvas().repaint();
    getMagicanvas()
      .addMouseListener(
        new MouseAdapter() {
          public void mousePressed(MouseEvent e) {
            if (e.getX() > BeginBean.getBeginX()) {
              if (
                (
                  e.getX() <
                  BeginBean.getBeginX() +
                  AgainAction.this.begin.getBeginW()
                ) &&
                (e.getY() > BeginBean.getBeginY())
              ) {
                if (
                  e.getY() < BeginBean.getBeginY() + BeginBean.getBeginH()
                ) BeginBean.setBeginY(BeginBean.getBeginOldY() + 3);
              }
            }
          }

          public void mouseReleased(MouseEvent e) {
            if (e.getX() > BeginBean.getBeginX()) {
              if (
                (
                  e.getX() <
                  BeginBean.getBeginX() +
                  AgainAction.this.begin.getBeginW()
                ) &&
                (e.getY() > BeginBean.getBeginY())
              ) {
                if (e.getY() < BeginBean.getBeginY() + BeginBean.getBeginH()) {
                  BeginBean.setBeginY(BeginBean.getBeginOldY());
                  if (!BirdBean._isAlive()) {
                    System.out.println("-> Again");
                    VoicePlayer.play("/res/sounds/sfx_swooshing.wav");
                    new ButtonEvent().event("Again");
                  }
                }
              }
            }
          }
        }
      );
    while (Switch._isContinue()) try {
      Thread.sleep(200L);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}
